const { login } = require("../controller/user");
const { SuccessModel, ErrorModel } = require("../model/resModel");

const handleUserRouter = (req, res) => {
  const method = req.method;
  const PREFIX = "/api/user/";

  // 登录
  if (method === "POST" && req.path === `${PREFIX}login`) {
    const { username, password } = req.body;

    const result = login(username, password);
    return result.then((data) => {
      if (data.username) {
        // 设置session
        req.session.username = data.username;
        req.session.realname = data.realname;
        return new SuccessModel();
      }
      return new ErrorModel("登录失败");
    });
  }

  // 登录验证测试
  // if (method === "GET" && req.path === `${PREFIX}login-test`) {
  //   if (req.session.username) {
  //     return Promise.resolve(
  //       new SuccessModel({ session: req.session })
  //     );
  //   }
  //   return Promise.resolve(new ErrorModel("尚未登录"));
  // }
};

module.exports = handleUserRouter;
